define([
	'../libs/zepto',
	'../common/base',
	'../common/common_click',
	'../common/dialog',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/ajax'
],function(){
	var freeTry = G_Prg.getCookie('freeTry');
	var m1 = Number(G_Prg.getQueryString('type'));
	var whole_height;
	var _isGetTime = 0;
	if(m1){
		whole_height = m1;
	}else{
		whole_height = window.innerHeight;
	}
	
	G_Prg.setCookie('first','1');
	this.init = function(){
		setCss() // 设置样式 
		$('.tab').click(function(){
			window.location.href = '/exam-time';
		});
		
		// input的聚焦失焦事件
		$('.ser_img').click(function(){
			var value = $('.text').val();
			if(value == ''){
				G_Prg.alert('搜索内容不能为空');
			}else{
				window.location.href = '/searchExam?fromUrl=doExam-index&keyWord='+value;
			}
		});

			
		// 做题
		$('.ullist li').eq(0).click(function(){
			window.location.href = '/doExam-index';
		});
		// 发现
		$('.ullist li').eq(2).click(function(){
			if (!userID) {
	            var yesCallback = function () {
	                parent.location.href = '/login?fromUrl=doExam-index';
	            }
	            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
	            return;
	        }
			window.location.href = '/find';
		});
		// 我的
		$('.ullist li').eq(3).click(function(){
			if(freeTry == 'true'){
				var yesCallback = function () {
	                parent.location.href = '/login?fromUrl=doExam-index&type=free';
	            }
	            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
	            return;
			}else{
				if (!userID) {
		            var yesCallback = function () {
		                parent.location.href = '/login?fromUrl=doExam-index';
		            }
		            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
		            return;
		        }
				window.location.href = '/userHome';
			}
		});
		// 巩固
		$('.ullist li').eq(1).click(function(){
			if(freeTry == 'true'){
				var yesCallback = function () {
	                parent.location.href = '/login?fromUrl=doExam-index&type=free';
	            }
	            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
	            return;
			}else{
				if (!userID) {
		            var yesCallback = function () {
		                parent.location.href = '/login?fromUrl=doExam-index';
		            }
		            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
		            return;
		        }
				window.location.href = '/Nurse';
			}
		});
		
	}
	
	// 设置样式
	function setCss(){
		var head_height = $('.header').height();
		var foot_height = $('.footer').height();
		$('.footer').css('top',whole_height-foot_height);
		var contain_height = whole_height - head_height - foot_height;
		$('.index-containt').css('height',contain_height);
		var s1 = '<div class="search"><div class="sear">'+
				'<div class="ser_img"><img src="/images/searchb.png" /></div>'+ 
				'<input type="text" placeholder="请输入试题关键字" class="text ser"/></div></div>';
		$('.index-containt').append(s1);
		var index_height = contain_height - $('.search').height();
		var s2 = '<div class="index"></div>';
		$('.index-containt').append(s2);
		// 首页
		$('.index').css('height',index_height);
		  
		//$('#loadingDiv').css('display','block');
		// 创建时间
		var timeStr = '<div class="distance_time"></div>';
		$('.index').append(timeStr);
		$('.distance_time').css('height',index_height* 0.3);
		var str1 = '<span>距离考试</span><p class="tab">'+
				'<b class="time">0</b><img src="/images/timeline.png"/></p><span>天</span>';
		$('.distance_time').html(str1);
		_showExamTime();	 
		setModule(); // 展示四个模块
	}
	
	// 创建首页的四个模块
	function setModule (){
		var four_height = $('.index').height() - $('.distance_time').height();
		var moduleStr = '<div class="four_module"></div>';
		$('.index').append(moduleStr);
		$('.four_module').css('height',four_height);
		var str3 = '<div class="main_position"><dl class="content_dllist">'+
				'<dt><img src="/images/read.png" alt=""+/></dt><dd><h4>章节练习</h4></dd></dl></div>';
		$('.four_module').append(str3);
		var str2 = '<div class="main_position"><dl class="content_dllist">'+
				'<dt><img src="/images/edit.png" alt=""/></dt><dd><h4>智能练习</h4></dd></dl></div>';
		$('.four_module').append(str2);
		var str4 = '<div class="main_position"><dl class="content_dllist">'+
				'<dt><img src="/images/simulation.png" alt=""/></dt><dd><h4>模拟考试</h4></dd></dl></div>';
		$('.four_module').append(str4);
		var str5 = '<div class="main_position"><img src="/images/choubei.png" class="choubei"/><dl class="content_dllist">'+
				'<dt><img src="/images/explain.png" alt=""/></dt><dd><h4>考点精讲</h4></dd></dl></div>';
		$('.four_module').append(str5);
		if(G_Cookie.getAppEName() == 'GJZC_ZYX' || G_Cookie.getAppEName() == 'GJZC_ZG_ZYX'){
			$('.choubei').attr('src','/images/new1.png');
		}
		// 点击事件
		_clickEvent();
	}
	
	function _clickEvent(){
		
		$('.main_position').eq(1).click(function(){
			var userID = G_Cookie.getUserID();
			if(freeTry == 'true'){
				var yesCallback = function () {
	                parent.location.href = '/login?fromUrl=doExam-index&type=free';
	            }
	            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
	            return;
			}else{
				_mockExamClick();
				if(userID && G_Prg.checkVipApp()){
					window.location.href = 'answerExam?fromUrl=doExam-index&count=15&wrongFirst=0&newFirst=0';
				}
			}
		});
		
		$('.main_position').eq(0).click(function(){
			window.location.href = '/chapterExam';
		});
		
		$('.main_position').eq(2).click(function(){
			var userID = G_Cookie.getUserID();
			if(freeTry == 'true'){
				var yesCallback = function () {
	                parent.location.href = '/login?fromUrl=doExam-index&type=free';
	            }
	            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
	            return;
			}else{
				_mockExamClick()
				if(userID && G_Prg.checkVipApp()){
					parent.location.href = window.location.href = '/mockExam';
				}
			}
		});
		
		
		
		$('.main_position').eq(3).click(function(){
			var userID = G_Cookie.getUserID();
			if(freeTry == 'true'){
				var yesCallback = function () {
	                window.location.href = '/login?fromUrl=doExam-index&type=free';
	           	}
				G_Prg.confirm('该功能登录后才能使用！', yesCallback);
			}else{
				if(G_Cookie.getAppEName() !== 'GJZC_ZYX' && G_Cookie.getAppEName() !== 'GJZC_ZG_ZYX'){
//					var yesCallback = function () {
//		                window.location.href = '/doExam-index';
//		           	}
					G_Prg.alert('功能正在拼命筹备中，敬请期待！');
					return;
				}else{
//					_mockExamClick();
//					if(userID && G_Prg.checkVipApp()){
//						window.location.href = '/TestingCentre';
//					}
					window.location.href = '/TestingCentre';
				}
			}
		});
		 
	}
	
	function _mockExamClick(){
        var userID = G_Cookie.getUserID();
        if (!userID) {
            var yesCallback = function () {
                parent.location.href = '/login?fromUrl=doExam-index';
            }
            G_Prg.confirm('该功能登录后才能使用！', yesCallback);
            return;
        }
        if (!G_Prg.checkVipApp()) {
			G_Prg.confirm('该功能充值后才能使用！', function(){
        		window.location.href = '/recharge'
            });
            return;
        }
    }
	
	// 获取距离考试的时间
   	function _showExamTime() {
		// var _userID = G_Cookie.getUserID();
  //  		if (!_userID) {//未登录
  //           return;
  //       }
  //       if (_isGetTime) {
  //           return;
  //       }
        var userName = G_Cookie.getUserName(); // 手机号
        var guid = G_Cookie.getGuid();
        //异步取考试时间
        var callBack = function (examTime) {
            if (examTime) {
                _time = Math.ceil((new Date(examTime).valueOf() - new Date().valueOf()) / (24 * 60 * 60 * 1000));
                if (_time < 0) {
                    _time = 0;
                }
            }
            $('#loadingDiv').css('display','none');
            $('.time').html(_time);	//考试时间设置
        }
        var appID = G_Cookie.getAppID();
        var userID = G_Cookie.getUserID();
        _getExamTime(userID, appID, guid, userName, callBack)
    }
   	
   	function _getExamTime(userID, appID, guid, userName, callBack) {
        // _isGetTime = 1;
        var URL = '/api/examTimeApi/getExamTimeData/' + userID + '/' + appID + '/' + guid + '/' + userName;
        var examTime = '';
        G_AjaxApi_Record.get(URL, true, function (json) {
            var jsonData = JSON.parse(json);
            if (jsonData.status === 200) {
                examTime = jsonData.data;
                callBack(examTime);
            } else if (jsonData.status === 201) {//无用户自己设置的时间
                //判断有没有官方考试时间
                if (jsonData.examBeginTime) {
                    examTime = jsonData.examBeginTime;
                    callBack(examTime);
                }
            } else if (jsonData.status === 300) {
                //G_maiDian.setBugRecordInfos('程序错误', '九宫格——获取考试时间', jsonData.status + ':' + jsonData.msg);
                G_Prg.throw('程序运行错误:_getExamTime,数据库连接错误');
            } else if (jsonData.status >= 400 && jsonData.status < 500) {
               // G_maiDian.setBugRecordInfos('业务逻辑错误', '九宫格——获取考试时间', jsonData.status + ':' + jsonData.msg);
                G_Prg.throw('程序运行错误:_getExamTime,参数错误');
            } else {
                //G_maiDian.setBugRecordInfos('其它程序错误', '九宫格——获取考试时间', jsonData.status + ':' + jsonData.msg);
                G_Prg.throw('程序运行错误:_getExamTime  未知返回值：' + JSON.stringify(jsonData));
            }
        });
    }
   	
//	if(navigator.onLine){
		init();
//	}else{
//		$('.wrap').html('网络无法连接，请连上网络之后点击刷新');
//	}
});




        		



